/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. In
** addition, as a special exception, Trolltech gives you certain
** additional rights. These rights are described in the Trolltech GPL
** Exception version 1.2, which can be found at
** http://www.trolltech.com/products/qt/gplexception/ and in the file
** GPL_EXCEPTION.txt in this package.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include <QBrush>
#include <QColorDialog>
#include <QPainter>
#include <QPalette>
#include <QStyle>
#include <QStyleOptionFrame>

#include "colorbutton.h"

ColorButton::ColorButton(QWidget *parent, QColor color) : QWidget(parent)
{
    setFocusPolicy(Qt::StrongFocus);
    m_color = color;
}

QColor ColorButton::getColor()
{
    return m_color;
}

void ColorButton::setColor(QColor color)
{
    m_color = color;
    update();
}

/**
 * sizeHint based on sizeHint from QWellArray in qcolordialog.cpp
 * from Qt 4.3.4 source.
 */
QSize ColorButton::sizeHint() const
{
    ensurePolished();
    return QSize(64, 48);
}

void ColorButton::keyReleaseEvent(QKeyEvent *event)
{
    if (event->key() == Qt::Key_Space) {
            showColorDialog();
    }
}

void ColorButton::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);

    showColorDialog();
}

/**
 * paintEvent based on paintCell from QWellArray in qcolordialog.cpp
 * from Qt 4.3.4 source.
 */
void ColorButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    // Margin used in QColorDialog, QWellArray internal class.
    int margin = 3;

    QStyleOptionFrame styleOptionFrame;
    QPainter painter(this);

    // Set the style of the frame.
    styleOptionFrame.lineWidth = style()->pixelMetric(
        QStyle::PM_DefaultFrameWidth);
    styleOptionFrame.midLineWidth = 1;
    styleOptionFrame.rect = painter.window().adjusted(margin, margin, -margin,
        -margin);
    styleOptionFrame.palette = palette();
    styleOptionFrame.state = QStyle::State_Enabled | QStyle::State_Sunken;
    // Draw the frame.
    style()->drawPrimitive(QStyle::PE_Frame, &styleOptionFrame, &painter,
        this);

    margin = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);

    // Draw a focus rectangle around the widget if it is selected.
    if (hasFocus()) {
        // Set the look of the focus rectangle.
        QStyleOptionFocusRect styleOptionFocusRect;
        styleOptionFocusRect.palette = palette();
        styleOptionFocusRect.rect = painter.window();
        styleOptionFocusRect.state = QStyle::State_None
            | QStyle::State_KeyboardFocusChange;
        // Draw a focus rectangle around the widget.
        style()->drawPrimitive(QStyle::PE_FrameFocusRect,
            &styleOptionFocusRect, &painter, this);
    }

    // Fill the interior with the color selected for this widget.
    painter.fillRect(styleOptionFrame.rect.adjusted(margin, margin, -margin,
        -margin), QBrush(m_color));
}

void ColorButton::showColorDialog()
{
    QColor color = QColorDialog::getColor(m_color, this);

    if (color.isValid()) {
        m_color = color;
        update();
    }
}
